<?php
namespace Home\Controller;
use Think\Controller;

class LoginController extends Controller {
    //登陆主页
    public function index(){
        $this->display();
    }
  
    public function reg(){
      if(!IS_POST){
        $this->display();
      }else if(IS_POST){
        $phone=$_POST['Phone'];
        $ishave=M('users')->where('phone="'.$phone.'"')->count();
        if ($ishave!=0) {
          echo "<script>alert("该手机号已注册");
        window.location.reload();
          </script>";
          die();
        }else{
          $data['phone']=$phone;
          $data['password']=md5($_POST['password']);
          M('users')->add($data);
          echo "<script>alert("注册成功");
        window.location.href='http://hfd.fangbanghui.com/index.php?m=Home&c=Login&a=index';
          </script>";
        }
      }
    }

    public function login(){
      $phone=$_POST['username'];
      $password=md5($_POST['password']);
      $pw=M('users')->where('phone="'.$phone.'"')->find();
      if($password!=$pw['password']){
        echo "<script>alert("用户名或密码错误");
        window.location.reload();
          </script>";
      }else{
        echo "<script>alert("登录成功");
        window.location.href='http://hfd.fangbanghui.com';
          </script>";
        $_SESSION['adminId']=$phone;
      }
    }
}